package com.mudalov.rec.dao.hibernate.impl;

import java.util.List;
import org.hibernate.SessionFactory;

import com.mudalov.rec.dao.interfaces.HbnGenericDAO;
import com.mudalov.rec.dao.interfaces.IStudyProgramDAO;
import com.mudalov.rec.dao.model.study.StudyProgram;
import java.util.ArrayList;
import org.hibernate.Session;

public class HbnStudyProgramDAO extends HbnGenericDAO<StudyProgram, Long> implements
	IStudyProgramDAO {
	
	public HbnStudyProgramDAO(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	protected Class<StudyProgram> getEntityClass() {
		return StudyProgram.class;
	}

    public List<StudyProgram> getStudyPrograms(String name, String owner, String subject) {
        String query = "from StudyProgram s where s.teacher.fullName like :ownerName " +
            "and s.name like :name " +
            "and s.subject.text like :subjectText";
        Session session = sessionFactory.openSession();
        List<StudyProgram> result = session.createQuery(query)
                .setString("name", "%" + name + "%")
                .setString("ownerName", "%" + owner + "%")
                .setString("subjectText", "%" + subject + "%").list();
        List<StudyProgram> detachedResult = new ArrayList<StudyProgram>();
        for (StudyProgram test : result) {
            detachedResult.add(test.getClone());
        }
        session.close();
        return detachedResult; 
    }

}
